
//表单登录和注册验证  包含登录和注册、后台管理界面的表单验证
(function () {

	/*防止脚本注入的正则*/
	var pattern = /[\<script\>][\<\/script\>]/gi;

	//登录验证
	$(function () {

		var sign1 = true,
			$uTishi = $("#denglu .uTishi"),
			$pTishi = $("#denglu .pTishi");

			//文本框失去焦点后
		    $('form[id="denglu"] :input').keyup(function(){

				 var $parent = $(this).parent();
				 //验证用户名
				 if( $(this).is('#user') ){

				 		$(this).css({backgroundColor: "#BCE1E8", borderColor: "#D4D3D3"});
						if ( this.value === "") {
							$(".trueFalse", $parent).removeClass("onError").text("");
						} 
						else if ( pattern.test(this.value) || this.value.length < 8 || ( this.value !== "" && !/[0-9]{8,10}$/.test(this.value) ) ){					
	         				$(".trueFalse", $parent).removeClass("onSuccess").addClass("onError").text("请输入8或10位的学工号");
							$(".high", $parent).css({color: "red"});
						} 
						else if (this.value.length === 8 || this.value.length === 10) {
							sign1 = false;
						    $(".trueFalse", $parent).removeClass("onError").addClass("onSuccess").text("输入正确");
						    $(".high", $parent).css({color: "#747981"});
						} 
						else if (this.value.length === 9) {
	         				$(".trueFalse", $parent).removeClass("onSuccess").addClass("onError").text("请输入8或10位的学工号");
							$(".high", $parent).css({color: "red"});
						}
				 }
				 //验证密码
				 if( $(this).is('#password') ){

				 	$(this).css({backgroundColor: "#BCE1E8", borderColor: "#D4D3D3"});
					if ( this.value === "") {
						$(".trueFalse", $parent).removeClass("onError").text("");
					}
					else if( pattern.test(this.value) || this.value.length < 6 ){					
	     				$(".trueFalse", $parent).removeClass("onSuccess").addClass("onError").text("请输入至少6位的密码");
						$(".high", $parent).css({color: "red"});
					} 
					else {
						sign1 = false;
					    $(".trueFalse", $parent).removeClass("onError").addClass("onSuccess").text("输入正确");
						$(".high", $parent).css({color: "#747981"});
					}
				 }
			}).focus(function(){

				if ($(this).is('#user')) {
					$uTishi.hide();
					$(this).css({backgroundColor: "#BCE1E8", borderColor: "yellow"});
				} 
				else if($(this).is("#password")){
					$pTishi.hide();
					$(this).css({backgroundColor: "#BCE1E8", borderColor: "yellow"});
				};
			}).blur(function(){

				$(this).triggerHandler("keyup");
				if ($(this).is('#user') && $(this).val() === "") {
					$uTishi.show();
					$(this).css({backgroundColor: "#FFF", borderColor: "#D4D3D3"});
				} 
				else if($(this).is("#password") && $(this).val() === ""){
					$pTishi.show();
					$(this).css({backgroundColor: "#FFF", borderColor: "#D4D3D3"});
				};
			});//end blur

		//提交，登录最终验证。
		$('#send').click(function(){

			$("form[id='denglu'] :input.required").triggerHandler('keyup');
			var numError = $('form[id="denglu"] .onError').length,
				$user = $("#user"),
				$password1 = $("#password"),
				options = {
					type: "POST",
					contentType: "application/json; charset=utf-8", 
					url: "",
					data: "",
					dataType: "json"
				};
			if(numError){
				return false;
			} 
			else {

				if (sign1){
					alert("请正确填写全部登录信息！");
					return false;
				} 
				else {
					if ($user.val().length >= 8) {
						options.url = "/user/login.do";
						options.data = JSON.stringify({
							u_id:$user.val(),
							u_passwd:$password1.val()
						});
						options.success = function (data) { 

							if (data) {

								location.href = "../registerSuccess.html";
							} 
							else {

								location.href = "../registerFail.html";
							}
		                }

						$.ajax(options);
					} 
					else {
						return false;
					};
					sign1 = true;
				}
			}
			return false;	 //如何防止连续点击？？？？
		});

		//重置
		$('#cancel').click(function(){
			// 样式还原
			$("#content .high").css({color: "#747981"});
			$("#content .trueFalse").removeClass().text("").addClass('trueFalse trueFalseNone');
			$uTishi.show();
			$pTishi.show();
			$("#user").css({backgroundColor: "#FFF"});
			$("#password").css({backgroundColor: "#FFF"});
		});
	});

	//注册验证
	$(function () {

		var sign = true,
			$studentID = $("#studentID"),
			$classID = $("#classID"),
			$password1 = $("#password1"),
			$studentName = $("#studentName"),
			$email = $("#email"),
			$telNum = $("#telNum"),

			options = {
				type: "POST",
				contentType: "application/json; charset=utf-8", 
				data: "",
				dataType: "json",
				success: function (data) { 
                	alert(data);
                 },
                 error: function (err) { 
                     alert("err:" + err);
                 }
			};


		function clickSubmit(target) {

			var numError = 0;
			// alert(target.id);
			$(target).css({backgroundColor: "#4DB1F6", borderColor: "#D4D3D3"});

			if (target.id === "sendz") {

				$("form[id='zhuce'] :input").triggerHandler('keyup');
				numError = $('form[id="zhuce"] .onError').length;
			}

			if(numError){
				// alert("用户已存在，注册失败！");
				return false;
			} 
			else {
				if (sign){
					alert("请填写全部注册信息！");
					return false;
				} 
				else {

					options.url = "/user/regin.do";
					options.data = JSON.stringify({
						u_id:$studentID.val(),
						u_passwd:$password1.val()
					});
					options.success = function (data) { 

	                	typeof data === boolean && data === true ? 
	                		location.href = "../registerSuccess.html" : 
	                		alert("用户已存在，注册失败!");
	                }

					$.ajax(options);
					sign = true;
				}					
			} 
		}

		function clickReset(target) {

			// 样式还原
			$(target).css({backgroundColor: "#4DB1F6", borderColor: "#D4D3D3"});
			if (target.id === "cancelz") {

				$("#zhuce .trueFalse1").removeClass().text("").addClass('trueFalse1 trueFalseNone');
				$("#zhuce .uTishi").show();
			} 
			else if (target.className === "resetChoose" || target.className === "resetChoose") {

				$("#changeForm .trueFalse1").removeClass().text("").addClass('trueFalse1 trueFalseNone');
				$("#changeForm .uTishi").show(1000);
			};
		}

		function xinXiYanZheng(target) {
			var $targetThis = $(target),
		 		$parent = $targetThis.parent();

		  //验证studentID
			 if( $targetThis.is('#studentID') ){
			 	$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
					if ( target.value === "") {
						$(".trueFalse1", $parent).removeClass("onError").text("");
					}
					else if( pattern.test(this.value) || target.value.length < 8 || ( target.value !== "" && !/[0-9]{10}$/.test(target.value) ) ){					
      					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入学号");
					} 
					else {
						sign = false;
					    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入正确");
					}
			 }

			//验证classID
			if( $targetThis.is('#classID') ){
				
				$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
				if ( target.value === "") {
					$(".trueFalse1", $parent).removeClass("onError").text("");
				}
				else if( pattern.test(this.value) || target.value.length <= 6 || ( target.value !== "" && !/(^[a-zA-Z]{1,2}[0-9]{3,5}$)|([0-9]{7}$)/.test(target.value) ) ){					
   					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入班级号");
				} 
				else {
					sign = false;
				    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入正确");
				}
  			}

			//验证password
			if( $targetThis.is('#password1') ){

				$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
				if ( target.value === "") {
					$(".trueFalse1", $parent).removeClass("onError").text("");
				}
				else if( pattern.test(this.value) || target.value.length < 6 ){					
   					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入至少6位密码");
				} 
				else {
					sign = false;
				    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入完成");
				}
  			}

			//验证studentName
			if( $targetThis.is('#studentName') ){

				$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
				if ( target.value === "") {
					$(".trueFalse1", $parent).removeClass("onError").text("");
				}
				else if( pattern.test(this.value) || ( target.value !== "" && /(^[0-9])|([0-9]$)/.test(target.value) ) || target.value.length < 2 ){	
					// console.log(this.value.length + " "+ /^[0-9]*/.test(this.value));				
   					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入姓名");
				} 
				else {
					sign = false;
				    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入完成");
				}
  			}

			//验证Email
			if( $targetThis.is('#email') ){

				$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
				if ( target.value === "") {
					$(".trueFalse1", $parent).removeClass("onError").text("");
				}
				else if( pattern.test(this.value) || target.value ==="" || ( target.value !=="" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(target.value) ) ){					
   					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入Email");
				} 
				else {
					sign = false;
				    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入完成");
				}
  			}

			//验证telNum
			if( $targetThis.is('#telNum') ){

				$targetThis.css({backgroundColor: "#BCE1E8", border: "1px solid yellow"});
				if ( target.value === "") {
					$(".trueFalse1", $parent).removeClass("onError").text("");
				}
				else if( pattern.test(this.value) || target.value.length < 11 || ( target.value !== "" && !/(130)|(186)|(152)|(188)|(131)|(138)|(185)|(150)|(189)+[0-9]{8}$/.test(target.value) ) ){					
   					$(".trueFalse1", $parent).removeClass("onSuccess").addClass("onError").text("请输入电话号码");
				} 
				else {
					sign = false;
				    $(".trueFalse1", $parent).removeClass("onError").addClass("onSuccess").text("输入完成");
				}
  			}
		}

		function focusYanZheng(target) {
			var $targetThis = $(target);

			$targetThis.css({backgroundColor: "#BCE1E8", borderColor: "yellow"}).siblings(".uTishi").hide();
		}

		function blurYanZheng(target) {

			var $targetThis = $(target);

			$targetThis.triggerHandler("keyup");
			if ($targetThis.val() === "") {
				$targetThis.css({backgroundColor: "#FFF", borderColor: "#D4D3D3"}).siblings(".uTishi").show();
			} 
			else if ($targetThis.val() !=="") {
				if ($targetThis.is("#sendz") || $targetThis.is("#cancelz")) {
					$targetThis.css({backgroundColor: "#4db1f6", borderColor: "#D4D3D3"});
				} 
				else {
					$targetThis.css({backgroundColor: "#FFF", borderColor: "#D4D3D3"});
				}
			}
		}


        //登录和注册  文本框失去焦点后
	    $('form[id="zhuce"] :input').keyup(function(event){
			 xinXiYanZheng(event.target);
		}).focus(function(event){
			focusYanZheng(event.target);
		}).blur(function(event){
			blurYanZheng(event.target);
		})//end blur

        //老师和学生个人信息修改表单  文本框失去焦点后
	    $('form[id="changeForm"] :input').keyup(function(event){
			 xinXiYanZheng(event.target);
		}).focus(function(event){
			focusYanZheng(event.target);
		}).blur(function(event){
			blurYanZheng(event.target);
		})//end blur


		
		//提交，最终验证。
		$('#sendz').click(function(event){
			clickSubmit(event.target);
		});

		$('#cancelz').click(function(event){
			clickReset(event.target);
		});

	});
	
	// 登录的切换注册
	$(function () {

		var $content = $("#content"),
			$contentZhuce = $("#contentZhuce"),
			$toZhuce = $("#toZhuce"),
			$toDenglu = $("#toDenglu"),
			$header = $("#header");

		$toZhuce.click(function () {
			if ($contentZhuce.is(":visible")) {
				return false;
			} 
			else {

				//直接使用hide()和show()来进行动画切换  弹框的右边动画会“过头”~~~ ？？？？？
				$header.fadeToggle(1000);		
				$content.fadeToggle(1000, function () {
					$contentZhuce.fadeToggle(1000);
				});
			};
		});

		$toDenglu.click(function () {

			if ($content.is(":visible")) {
				return false;
			} 
			else {
				$contentZhuce.fadeToggle(1000, function () {
					$content.fadeToggle(1000);
					$header.fadeToggle(1000);
				});
			};
		});
	});


}());